Comparing Connectomes Across Taxa


Affinity Testing

Import Connectomes

In [1]:
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
#from graspologic.plot import heatmap
In [2]:
from grapgh_io import GraphIO

ciona, ciona_e_att, ciona_n_att, ciona_g_att  = GraphIO.load('ciona.json')
fafb, fafb_e_att, fafb_n_att, fafb_g_att = GraphIO.load('fafb.json')
worm, worm_e_att, worm_n_att, worm_g_att = GraphIO.load('worm_wiring_connectome_0_SL.json')
In [16]:
fafb_sides = []

def homophilic_att(graph, name):
    if name == 'ciona':
        for key in graph.edges:
            try:
                if graph.nodes[key[0]]['Side'] == graph.nodes[key[1]]['Side']:
                    graph.edges[key]['Homophilic'] = True
                else:
                    graph.edges[key]['Homophilic'] = False
            except:
                continue

    elif name == 'worm':
        for key in graph.edges:
            try:
                if graph.nodes[key[0]]['attr_dict']['hemisphere'] == graph.nodes[key[1]]['attr_dict']['hemisphere']:
                    graph.edges[key]['Homophilic'] = True
                else:
                    graph.edges[key]['Homophilic'] = False
            except:
                continue
    
    elif name == 'fafb':
        for key in graph.edges:
                if graph.nodes[key[0]]['annotation'][0][-2] == 'L':
                    graph.nodes[key[0]]['Side'] = 'L'
                    if graph.nodes[key[0]]['annotation'][0][-2] == graph.nodes[key[1]]['annotation'][0][-2]:
                        graph.edges[key]['Homophilic'] = True
                        graph.nodes[key[1]]['Side'] = 'L'
                        fafb_sides.append(key[0])
                        fafb_sides.append(key[1])
                    elif graph.nodes[key[0]]['annotation'][0][-2] == 'R':
                        graph.nodes[key[0]]['Side'] = 'R'
                        graph.edges[key]['Homophilic'] = False
                        fafb_sides.append(key[0])
                        fafb_sides.append(key[1])
                    else:
                        graph.edges[key]['Homophilic'] = False
                elif graph.nodes[key[0]]['annotation'][0][-2] == 'R':
                    graph.nodes[key[0]]['Side'] = 'R'
                    if graph.nodes[key[0]]['annotation'][0][-2] == graph.nodes[key[1]]['annotation'][0][-2]:
                        graph.edges[key]['Homophilic'] = True
                        graph.nodes[key[1]]['Side'] = 'R'
                        fafb_sides.append(key[0])
                        fafb_sides.append(key[1])
                    elif graph.nodes[key[0]]['annotation'][0][-2] == 'L':
                        graph.nodes[key[0]]['Side'] = 'L'
                        graph.edges[key]['Homophilic'] = False
                        fafb_sides.append(key[0])
                        fafb_sides.append(key[1])
                    else:
                        graph.edges[key]['Homophilic'] = False
                else:
                    graph.edges[key]['Homophilic'] = False


def homotopic_att(graph, name):
    if name == 'ciona':
        for key in graph.edges:
            try:
                # Originally with cell type, but not enough samples
                if graph.nodes[key[0]]['Brain Region'] == graph.nodes[key[1]]['Brain Region'] and graph.edges[key]['Homophilic'] == False: 
                    graph.edges[key]['Homotopic'] = True
                else:
                    graph.edges[key]['Homotopic'] = False
            except:
                continue
    
    elif name == 'worm':
        for key in graph.edges:
            try:
                if (graph.nodes[key[0]]['attr_dict']['ID'][:-1] == graph.nodes[key[1]]['attr_dict']['ID'][:-1]) and (graph.nodes[key[0]]['attr_dict']['ID'][-1] != graph.nodes[key[1]]['attr_dict']['ID'][-1]) and graph.edges[key]['Homophilic'] == False:
                    graph.edges[key]['Homotopic'] = True
                else:
                    graph.edges[key]['Homotopic'] = False
            except:
                continue

    elif name == 'fafb':
        for key in graph.edges:
            try:
                if (graph.nodes[key[0]]['annotation'][0][:-2] == graph.nodes[key[1]]['annotation'][0][:-2]) and (graph.nodes[key[0]]['annotation'][0][-1] == graph.nodes[key[1]]['annotation'][0][-1]) and graph.edges[key]['Homophilic'] == False:
                    graph.edges[key]['Homotopic'] = True
                else:
                    graph.edges[key]['Homotopic'] = False
            except:
                continue
        



homophilic_att(ciona, 'ciona')
homotopic_att(ciona, 'ciona')

homophilic_att(worm, 'worm')
homotopic_att(worm, 'worm')

homophilic_att(fafb, 'fafb')
homotopic_att(fafb, 'fafb')
In [ ]:
# if (worm.nodes[key[0]]['attr_dict']['ID'][-1] == 'L' or worm.nodes[key[0]]['attr_dict']['ID'][-1] == 'R') and (worm.nodes[key[1]]['attr_dict']['ID'][-1] == 'L' or worm.nodes[key[1]]['attr_dict']['ID'][-1] == 'R')

# graph.nodes[key[1]]['annotation'][0][:-1]) and (graph.nodes[key[0]]['attr_dict']['ID'][-1] != graph.nodes[key[1]]['attr_dict']['ID'][-1]) and graph.edges[key]['Homophilic'] == False:

Ex:

In [34]:
worm.edges[(18, 19, 0)]
Out[34]:
{'attr_dict': {'weight': 7}, 'Homophilic': False, 'Homotopic': True}
In [33]:
worm.nodes[19]
Out[33]:
{'attr_dict': {'ID': 'NSMR',
  'hemisphere': 'right',
  'dorsoventral': None,
  'cell_type0': 'PHARYNX',
  'cell_type1': None}}
In [31]:
worm.nodes[18]
Out[31]:
{'attr_dict': {'ID': 'NSML',
  'hemisphere': 'left',
  'dorsoventral': None,
  'cell_type0': 'PHARYNX',
  'cell_type1': None}}
In [20]:
import random

def find_sides(graph, name):
    affinity_nodes = []
    positions = {}
    y_pos = 1
    x_left_pos = -20
    x_right_pos = 20
    for key in graph.nodes:
        try:
            if name == 'ciona' or 'fafb':
                if graph.nodes[key]['Side'] == 'L' :
                    affinity_nodes.append(key)
                    positions[key] = np.asarray([x_left_pos + random.uniform(-5, 5), y_pos])
                elif graph.nodes[key]['Side'] == 'R':
                    affinity_nodes.append(key)
                    positions[key] = np.asarray([x_right_pos + random.uniform(-5, 5), y_pos])
            elif name == 'worm':
                if graph.nodes[key]['attr_dict']['hemisphere'] == 'left' :
                    affinity_nodes.append(key)
                    positions[key] = np.asarray([x_left_pos + random.uniform(-5, 5), y_pos])
                elif graph.nodes[key]['attr_dict']['hemisphere'] == 'right' :
                    affinity_nodes.append(key)
                    positions[key] = np.asarray([x_right_pos + random.uniform(-5, 5), y_pos])
            

            y_pos += 1

        except:
            y_pos += 1
            continue

    return affinity_nodes, positions

ciona_sides, ciona_coors = find_sides(ciona, 'ciona')

worm_sides, worm_coors = find_sides(worm, 'worm')

fafb_sides, fafb_coors = find_sides(fafb, 'fafb')

Visualize Each

Ciona

In [16]:
#Ciona: Homophilic in White, Non-Homophilic in Black
#Left hemisphere on the left, right on the right

edge_colors = []
for line in nx.generate_edgelist(ciona.subgraph(ciona_sides), data=False):
    edge = []
    edge.append(line.rsplit(' ', 1)[0])
    edge.append(line.rsplit(' ', 1)[1])
    edge = tuple(edge)
    
    if ciona.subgraph(ciona_sides).edges[edge]['Homophilic']:
        edge_colors.append("white")
    else:
        edge_colors.append("black")

fig = plt.figure(figsize=(5, 5))
ax = fig.add_subplot(1, 1, 1) # nrows, ncols, index
ax.set_facecolor((0.5, 0, 0))
nx.draw_networkx(ciona.subgraph(ciona_sides), pos=ciona_coors, node_size=20, with_labels=False, width=0.25, arrowsize=5, edge_color=edge_colors) #, nodelist=ciona_sides
2021-04-20T12:17:17.102237 image/svg+xml Matplotlib v3.3.0, https://matplotlib.org/
In [19]:
#Ciona: Non-Homotopic in White, Homotopic in Black
#Left hemisphere on the left, right on the right

edge_colors = []
for line in nx.generate_edgelist(ciona.subgraph(ciona_sides), data=False):
    edge = []
    edge.append(line.rsplit(' ', 1)[0])
    edge.append(line.rsplit(' ', 1)[1])
    edge = tuple(edge)
    
    if ciona.subgraph(ciona_sides).edges[edge]['Homotopic']:
        edge_colors.append("black")
    else:
        edge_colors.append("white")

fig = plt.figure(figsize=(5, 5))
ax = fig.add_subplot(1, 1, 1) # nrows, ncols, index
ax.set_facecolor((0.5, 0, 0))
nx.draw_networkx(ciona.subgraph(ciona_sides), pos=ciona_coors, node_size=20, with_labels=False, width=0.25, arrowsize=5, edge_color=edge_colors) #, nodelist=ciona_sides
2021-04-20T12:17:51.525032 image/svg+xml Matplotlib v3.3.0, https://matplotlib.org/

Worm

In [43]:
#Worm: Homophilic in White, Non-Homophilic in Black
#Left hemisphere on the left, right on the right

edge_colors = []
for edge in worm.subgraph(worm_sides).edges:

    if worm.subgraph(worm_sides).edges[edge]['Homophilic']:
        edge_colors.append("white")
    else:
        edge_colors.append("black")

fig = plt.figure(figsize=(5, 5))
ax = fig.add_subplot(1, 1, 1) # nrows, ncols, index
ax.set_facecolor((0.5, 0, 0))
nx.draw_networkx(worm.subgraph(worm_sides), pos=worm_coors, node_size=20, with_labels=False, width=0.25, arrowsize=5, edge_color=edge_colors) #, nodelist=ciona_sides
2021-04-20T12:55:43.269298 image/svg+xml Matplotlib v3.3.0, https://matplotlib.org/
In [11]:
#Worm: Non-Homotopic in White, Homotopic in Black
#Left hemisphere on the left, right on the right

edge_colors = []
for edge in worm.subgraph(worm_sides).edges:

    if worm.subgraph(worm_sides).edges[edge]['Homotopic']:
        edge_colors.append("black")
    else:
        edge_colors.append("white")

fig = plt.figure(figsize=(5, 5))
ax = fig.add_subplot(1, 1, 1) # nrows, ncols, index
ax.set_facecolor((0.5, 0, 0))
nx.draw_networkx(worm.subgraph(worm_sides), pos=worm_coors, node_size=20, with_labels=False, width=0.25, arrowsize=5, edge_color=edge_colors) #, nodelist=ciona_sides
2021-04-20T13:13:44.544558 image/svg+xml Matplotlib v3.3.0, https://matplotlib.org/

FAFB

In [21]:
#Fafb: Homophilic in White, Non-Homophilic in Black
#Left hemisphere on the left, right on the right

edge_colors = []
for line in nx.generate_edgelist(fafb.subgraph(fafb_sides), data=False):
    edge = []
    edge.append(int(line.rsplit(' ', 1)[0]))
    edge.append(int(line.rsplit(' ', 1)[1]))
    edge = tuple(edge)
    
    if fafb.subgraph(fafb_sides).edges[edge]['Homophilic']:
        edge_colors.append("white")
    else:
        edge_colors.append("black")

fig = plt.figure(figsize=(5, 5))
ax = fig.add_subplot(1, 1, 1) # nrows, ncols, index
ax.set_facecolor((0.5, 0, 0))
nx.draw_networkx(fafb.subgraph(fafb_sides), pos=fafb_coors, node_size=20, with_labels=False, width=0.25, arrowsize=5, edge_color=edge_colors) #, nodelist=ciona_sides
2021-04-20T13:57:16.569331 image/svg+xml Matplotlib v3.3.0, https://matplotlib.org/
In [22]:
#Fafb: Non-Homotopic in White, Homotopic in Black
#Left hemisphere on the left, right on the right

edge_colors = []
for line in nx.generate_edgelist(fafb.subgraph(fafb_sides), data=False):
    edge = []
    edge.append(int(line.rsplit(' ', 1)[0]))
    edge.append(int(line.rsplit(' ', 1)[1]))
    edge = tuple(edge)
    
    if fafb.subgraph(fafb_sides).edges[edge]['Homotopic']:
        edge_colors.append("black")
    else:
        edge_colors.append("white")

fig = plt.figure(figsize=(5, 5))
ax = fig.add_subplot(1, 1, 1) # nrows, ncols, index
ax.set_facecolor((0.5, 0, 0))
nx.draw_networkx(fafb.subgraph(fafb_sides), pos=fafb_coors, node_size=20, with_labels=False, width=0.25, arrowsize=5, edge_color=edge_colors) #, nodelist=ciona_sides
2021-04-20T13:58:34.166493 image/svg+xml Matplotlib v3.3.0, https://matplotlib.org/
In [23]:
!jupyter nbconvert --to html Affinity_Testing.ipynb
[NbConvertApp] WARNING | pattern 'your_notebook_name.ipynb' matched no files
This application is used to convert notebook files (*.ipynb) to various other
formats.

WARNING: THE COMMANDLINE INTERFACE MAY CHANGE IN FUTURE RELEASES.

Options
-------

Arguments that take values are actually convenience aliases to full
Configurables, whose aliases are listed on the help line. For more information
on full configurables, see '--help-all'.

--debug
    set log level to logging.DEBUG (maximize logging output)
--generate-config
    generate default config file
-y
    Answer yes to any questions instead of prompting.
--execute
    Execute the notebook prior to export.
--allow-errors
    Continue notebook execution even if one of the cells throws an error and include the error message in the cell output (the default behaviour is to abort conversion). This flag is only relevant if '--execute' was specified, too.
--stdin
    read a single notebook file from stdin. Write the resulting notebook with default basename 'notebook.*'
--stdout
    Write notebook output to stdout instead of files.
--inplace
    Run nbconvert in place, overwriting the existing notebook (only 
    relevant when converting to notebook format)
--clear-output
    Clear output of current file and save in place, 
    overwriting the existing notebook.
--no-prompt
    Exclude input and output prompts from converted document.
--no-input
    Exclude input cells and output prompts from converted document. 
    This mode is ideal for generating code-free reports.
--log-level=<Enum> (Application.log_level)
    Default: 30
    Choices: (0, 10, 20, 30, 40, 50, 'DEBUG', 'INFO', 'WARN', 'ERROR', 'CRITICAL')
    Set the log level by value or name.
--config=<Unicode> (JupyterApp.config_file)
    Default: ''
    Full path of a config file.
--to=<Unicode> (NbConvertApp.export_format)
    Default: 'html'
    The export format to be used, either one of the built-in formats
    ['asciidoc', 'custom', 'html', 'latex', 'markdown', 'notebook', 'pdf',
    'python', 'rst', 'script', 'slides'] or a dotted object name that represents
    the import path for an `Exporter` class
--template=<Unicode> (TemplateExporter.template_file)
    Default: ''
    Name of the template file to use
--writer=<DottedObjectName> (NbConvertApp.writer_class)
    Default: 'FilesWriter'
    Writer class used to write the  results of the conversion
--post=<DottedOrNone> (NbConvertApp.postprocessor_class)
    Default: ''
    PostProcessor class used to write the results of the conversion
--output=<Unicode> (NbConvertApp.output_base)
    Default: ''
    overwrite base name use for output files. can only be used when converting
    one notebook at a time.
--output-dir=<Unicode> (FilesWriter.build_directory)
    Default: ''
    Directory to write output(s) to. Defaults to output to the directory of each
    notebook. To recover previous default behaviour (outputting to the current
    working directory) use . as the flag value.
--reveal-prefix=<Unicode> (SlidesExporter.reveal_url_prefix)
    Default: ''
    The URL prefix for reveal.js (version 3.x). This defaults to the reveal CDN,
    but can be any url pointing to a copy  of reveal.js.
    For speaker notes to work, this must be a relative path to a local  copy of
    reveal.js: e.g., "reveal.js".
    If a relative path is given, it must be a subdirectory of the current
    directory (from which the server is run).
    See the usage documentation
    (https://nbconvert.readthedocs.io/en/latest/usage.html#reveal-js-html-
    slideshow) for more details.
--nbformat=<Enum> (NotebookExporter.nbformat_version)
    Default: 4
    Choices: [1, 2, 3, 4]
    The nbformat version to write. Use this to downgrade notebooks.

To see all available configurables, use `--help-all`

Examples
--------

    The simplest way to use nbconvert is
    
    > jupyter nbconvert mynotebook.ipynb
    
    which will convert mynotebook.ipynb to the default format (probably HTML).
    
    You can specify the export format with `--to`.
    Options include ['asciidoc', 'custom', 'html', 'latex', 'markdown', 'notebook', 'pdf', 'python', 'rst', 'script', 'slides'].
    
    > jupyter nbconvert --to latex mynotebook.ipynb
    
    Both HTML and LaTeX support multiple output templates. LaTeX includes
    'base', 'article' and 'report'.  HTML includes 'basic' and 'full'. You
    can specify the flavor of the format used.
    
    > jupyter nbconvert --to html --template basic mynotebook.ipynb
    
    You can also pipe the output to stdout, rather than a file
    
    > jupyter nbconvert mynotebook.ipynb --stdout
    
    PDF is generated via latex
    
    > jupyter nbconvert mynotebook.ipynb --to pdf
    
    You can get (and serve) a Reveal.js-powered slideshow
    
    > jupyter nbconvert myslides.ipynb --to slides --post serve
    
    Multiple notebooks can be given at the command line in a couple of 
    different ways:
    
    > jupyter nbconvert notebook*.ipynb
    > jupyter nbconvert notebook1.ipynb notebook2.ipynb
    
    or you can specify the notebooks list in a config file, containing::
    
        c.NbConvertApp.notebooks = ["my_notebook.ipynb"]
    
    > jupyter nbconvert --config mycfg.py